﻿// Controllers/BaseController.cs
using System.Globalization;
using System.Threading;
using System.Web.Mvc;
using WebApplication1.Utilities;

namespace WebApplication1.Controllers
{
    public class BaseController : Controller
    {
        protected override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            // 设置当前语言
            string cultureName = "zh-CN";

            try
            {
                CultureInfo culture = CultureInfo.CreateSpecificCulture(cultureName);
                Thread.CurrentThread.CurrentCulture = culture;
                Thread.CurrentThread.CurrentUICulture = culture;
            }
            catch
            {
                // 如果设置失败，使用默认文化
                CultureInfo culture = CultureInfo.CreateSpecificCulture("zh-CN");
                Thread.CurrentThread.CurrentCulture = culture;
                Thread.CurrentThread.CurrentUICulture = culture;
            }

            base.OnActionExecuting(filterContext);
        }

        // 切换语言的 Action
       
        [HttpPost]
        public ActionResult ChangeLanguage(string lang, string returnUrl = "")
        {
            try
            {
                // 设置语言
                WebApplication1.Utilities.LanguageManager.SetCurrentLanguage(lang);

                // 如果有返回URL，则重定向到该URL
                if (!string.IsNullOrEmpty(returnUrl))
                {
                    return Redirect(returnUrl);
                }

                // 否则重定向到首页
                return RedirectToAction("Index", "Home");
            }
            catch
            {
                return RedirectToAction("Index", "Home");
            }
        }
    }
}